//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __LOADER_H__
#define __LOADER_H__

EXTERN_C const InterfaceID EIID_IProxy;

#define MIN_BUFFER_SIZE 0x1000

typedef enum {
    LOADED_FILE_TYPE_NONE = 0,
    LOADED_FILE_TYPE_BMFS = 1,
    LOADED_FILE_TYPE_FILESYS = 2,
} LOADED_FILE_TYPE;

class CLoadedFile
{
public:
    CLoadedFile() : m_FileType(LOADED_FILE_TYPE_NONE),
        m_FileSize(0), m_pwszFullPath(NULL)
    {
        m_File.pBMFile = NULL;
        m_Buf.vaddr = NULL;
        m_Buf.nOrder = -1;
    }

    ~CLoadedFile();

    ECode Init(const wchar_t *wszName, CModule * pModule);

    ECode Init(const wchar_t *wszFullPath);


    ECode Read(UInt32 offset, UInt32 size, PVoid *ppBuf,
                 Boolean bUseInterBuf = TRUE);

    INLINE UInt32 GetSize() const { return m_FileSize;}

    INLINE Boolean HasProxy() const {
        return m_FileType == LOADED_FILE_TYPE_FILESYS && m_File.pIAbsoluteIO->Probe(EIID_IProxy);
    }

private:
    ECode RegainPageBuffer(Int32 nNeedOrder);

protected:
    LOADED_FILE_TYPE   m_FileType;
    union
    {
        Byte * pBMFile;
        IAbsoluteIO *pIAbsoluteIO;
    } m_File;
    UInt32   m_FileSize;
    struct PageBuffer
    {
        virtaddr_t vaddr;
        Int32  nOrder;
    } m_Buf;
    const wchar_t * m_pwszFullPath;
};

#endif // __LOADER_H__
